package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsTopicMapper;
import com.qf.pojo.DtsCouponUser;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
public class DtsTopicServiceImpl implements DtsTopicService {
    @Autowired
    DtsTopicMapper topicMapper;
    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, String title, String subtitle, String sort, String order) {

        IPage<DtsTopic> ipage = new Page<>(page, limit);
        QueryWrapper<DtsTopic> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)) {
            wrapper.eq("title",title);
        }
        if (!StringUtils.isEmpty(subtitle)) {
            wrapper.eq("subtitle",subtitle);
        }
        if ("desc".equals(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        topicMapper.selectPage(ipage,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("items", ipage.getRecords());
        data.put("total", ipage.getTotal());

        return data;
    }

    @Override
    public DtsTopic update(DtsTopic topic) {
        topic.setUpdateTime(new Date());
        topicMapper.updateById(topic);

        return topic;
    }

    @Override
    public DtsTopic create(DtsTopic topic) {
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        topic.setDeleted(false);
        topicMapper.insert(topic);
        return topic;
    }

    @Override
    public DtsTopic delete(DtsTopic topic) {
        topicMapper.deleteById(topic.getId());
        return topic;
    }
}